/**
 * 1. 登陆
 * 2. 请求一个接口，获取用户身份信息接口
 * 3. 封装过滤函数，递归调用
 * 4. addRoutes
 */
import { asyncRoutesMap } from '@/router/config'
import router from '@/router'
const login = {
	namespaced: true, // vue: namespaced  umi namespace:
	state: {
		asyncRoutes: []
	},
	mutations: {
		ASYNC_ADD_ROUTES(state, userid) {
			// 封装过滤函数
			function filterRoutes(config, userid) {
				// 1. 过滤一级路由
				const routerArr = config.filter(v => v.meta.userId.indexOf(userid) > -1);
				// 2. 过滤其他的路由
				config.forEach(item => {
					item.children && item.children.length && (item.children = filterRoutes(item.children))
				})
				return routerArr;
			}
			const arr = filterRoutes(asyncRoutesMap, userid)
			console.log('arr,', arr)
			state.asyncRoutes = arr;
			router.addRoutes(arr);
		}
	},
	getters: {
		GetAsyncRoutes(state) {
      return state.asyncRoutes
		}
	},
	actions: {
		GeneratorRoutes({ commit }) {
			return new Promise((resolve, reject) => {
				const userid = 2
				commit('ASYNC_ADD_ROUTES', userid)
			})
		}
	}
}

export { login }
